package com.dims_hsrc.web.controller.data;

import com.dims_hsrc.common.annotation.Log;
import com.dims_hsrc.common.core.controller.BaseController;
import com.dims_hsrc.common.core.domain.AjaxResult;
import com.dims_hsrc.common.enums.BusinessType;
import com.dims_hsrc.common.utils.poi.ExcelUtil;
import com.dims_hsrc.data.domain.DataDirectMapping;
import com.dims_hsrc.data.domain.DataDirectMappingResult;
import com.dims_hsrc.data.service.IDataDirectMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 直接映射Controller
 *
 * @author 赵浩然
 * @date 2024-03-18
 */
@RestController
@RequestMapping("/data/direct_mapping")
public class DataDirectMappingController extends BaseController {
    @Autowired
    private IDataDirectMappingService serv;

    /**
     * 查询映射列表
     */
    @GetMapping("/list")
    public AjaxResult list(DataDirectMapping mapping) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDirectMapping> list = serv.list(mapping);
        return success(list);
    }

    /**
     * 根据结构ID获取映射列表
     */
    @GetMapping("/struct/{sid}")
    public AjaxResult getBySid(@PathVariable("sid") String sid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDirectMappingResult> list = serv.getBySid(sid);
        return success(list);
    }

    /**
     * 根据结构需求ID获取映射列表
     */
    @GetMapping("/demand/{sdid}")
    public AjaxResult getBySdid(@PathVariable("sdid") String sdid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDirectMapping> list = serv.getBySdid(sdid);
        return success(list);
    }

    /**
     * 根据结构指标ID获取映射列表
     */
    @GetMapping("/indicator/{siid}")
    public AjaxResult getBySiid(@PathVariable("siid") String siid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDirectMapping> list = serv.getBySiid(siid);
        return success(list);
    }

    /**
     * 导出映射列表
     */
    @Log(title = "直接映射", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse resp, DataDirectMapping mapping) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDirectMapping> list = serv.list(mapping);
        ExcelUtil<DataDirectMapping> util = new ExcelUtil<DataDirectMapping>(DataDirectMapping.class);
        util.exportExcel(resp, list, "直接映射数据");
    }

    /**
     * 获取映射详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(serv.getOne(id));
    }

    /**
     * 新增直接映射
     */
    @Log(title = "直接映射", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataDirectMapping mapping) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(serv.add(mapping));
    }

    /**
     * 修改直接映射
     */
    @Log(title = "直接映射", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody DataDirectMapping mapping) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(serv.update(mapping));
    }

    /**
     * 删除映射
     */
    @Log(title = "直接映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(serv.remove(ids));
    }

    /**
     * 删除单个映射
     */
    @Log(title = "直接映射", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(serv.del(id));
    }

    /**
     * 检查文本类型需求是否已被映射
     */
    @GetMapping("/check/demand/{sdid}")
    public AjaxResult checkDemandMapped(@PathVariable("sdid") String sdid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        boolean mapped = serv.checkDemandMapped(sdid);
        return success(mapped);
    }
    
    /**
     * 检查文本类型技术指标是否已被映射
     */
    @GetMapping("/check/indicator/{siid}")
    public AjaxResult checkIndicatorMapped(@PathVariable("siid") String siid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        boolean mapped = serv.checkIndicatorMapped(siid);
        return success(mapped);
    }
    
    /**
     * 根据结构ID获取所有文本类型的映射
     */
    @GetMapping("/text/{sid}")
    public AjaxResult getTextTypeMappingsBySid(@PathVariable("sid") String sid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDirectMapping> list = serv.getTextTypeMappingsBySid(sid);
        return success(list);
    }
    
    /**
     * 检查需求和技术指标的组合是否已存在映射
     */
    @GetMapping("/check/exists")
    public AjaxResult checkMappingExists(@RequestParam("sid") String sid, 
                                         @RequestParam("sdid") String sdid, 
                                         @RequestParam("siid") String siid) {
        System.out.println(new Throwable().getStackTrace()[0]);
        boolean exists = serv.checkMappingExists(sid, sdid, siid);
        return success(exists);
    }
} 